import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import store from "../store/index"

const routes = [
  {
    path: '/',
    redirect:"/home"
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    redirect:"/home/users",
    component: () => import('../views/Home.vue'),
    children:[
      {
        path: 'users',
        name: 'Users',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "users" */ '../views/users/Users.vue')
      },
      {
        path: 'roles',
        name: 'Roles',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "roles" */ '../views/roles/Roles.vue')
      },
      {
        path: 'rights',
        name: 'Rights',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "rights" */ '../views/rights/Rights.vue')
      },
      {
        path: 'goods',
        name: 'Goods',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "goods" */ '../views/goods/Goods.vue')
      },
      {
        path: 'categories',
        name: 'Categories',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "categories" */ '../views/categories/Categories.vue')
      },
      {
        path: 'orders',
        name: 'Orders',
        component: () => import(/* webpackChunkName: "Orders" */ '../views/orders/Orders.vue')
      },
      {
        path: 'reports',
        name: 'Reports',
        component: () => import(/* webpackChunkName: "Reports" */ '../views/reports/Reports.vue')
      },
      {
        path: 'params',
        name: 'Params',
        component: () => import(/* webpackChunkName: "params" */ '../views/params/Params.vue')
      },
      {
        path: 'addgoods',
        name: 'Addgoods',
        component: () => import(/* webpackChunkName: "addgoods" */ '../views/goods/Addgoods.vue')
      },


    ]

  },

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
//进入先走导航守卫，少了几次无效请求
router.beforeEach((to, from, next) => {
  if(!store.state.token && to.name!="Login"){
    next("/login")
    return
  }
  next()
  
})
export default router
